package org.jeecg.modules.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jeecg.modules.app.entity.AppClient;
import org.jeecg.modules.app.entity.AppMenu;
import org.jeecg.modules.app.mapper.AppClientMapper;
import org.jeecg.modules.app.mapper.AppMenuMapper;
import org.jeecg.modules.app.service.IAppMenuService;
import org.jeecg.modules.app.vo.MenuTreeVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description: 应用菜单
 * @Author: sean
 * @Date:   2024-10-25
 * @Version: V1.0
 */
@RequiredArgsConstructor
@Service
public class AppMenuServiceImpl extends ServiceImpl<AppMenuMapper, AppMenu> implements IAppMenuService {

    private final AppClientMapper appClientMapper;

    @Override
    public List<MenuTreeVO> selectJoinTree(Wrapper wrapper) {
        List<MenuTreeVO> appMenuList = baseMapper.selectJoinTree(wrapper);
        if(ObjectUtil.isNotEmpty(appMenuList)){
            Set<Long> clientIds = appMenuList.stream().map(MenuTreeVO::getClientId).collect(Collectors.toSet());
            List<AppClient> appClientList = appClientMapper.selectBatchIds(clientIds);
            appMenuList.forEach(item->{
                AppClient appClient = appClientList.stream()
                        .filter(a-> a.getId().equals(item.getClientId())).findFirst().orElse(null);
                if(ObjectUtil.isNotEmpty(appClient)){
                    item.setClientName(appClient.getName());
                }
            });
        }
        return appMenuList;
    }

//    @Override
//    public List<MenuTreeVO> selectJoinTree(AppMenu appMenu) {
//        List<MenuTreeVO> appMenuList = baseMapper.selectJoinTree(appMenu);
//        if(ObjectUtil.isNotEmpty(appMenuList)){
//            Set<Long> clientIds = appMenuList.stream().map(MenuTreeVO::getClientId).collect(Collectors.toSet());
//            List<AppClient> appClientList = appClientMapper.selectBatchIds(clientIds);
//            appMenuList.forEach(item->{
//                AppClient appClient = appClientList.stream()
//                        .filter(a-> a.getId().equals(item.getClientId())).findFirst().orElse(null);
//                if(ObjectUtil.isNotEmpty(appClient)){
//                    item.setClientName(appClient.getName());
//                }
//            });
//        }
//        return appMenuList;
//    }


}
